package main

import (
	"bufio"
	"database/sql"
	"fmt"
	_"github.com/go-sql-driver/mysql"
	"math"
	"os"
	"os/exec"
	"strconv"
	"strings"
)

var accountNum int64 = 10000
// chat_account  用户表 id, account, password, nickname
// chat_record  聊天记录表 id, speaker, listener, talk_time, talk_content

func BackUpToFile() {
  exec.Command("mysqldump -u godemo -p godemo -h 127.0.0.1 > backup.sql")
}

func Register(nickname string, password string) {
	// chat register --nickname=test2022 --password=password2022
	// Register nickname: test2022  password: password2022
	fmt.Println("Register nickname:", nickname, " password:", password)

	db, _ := sql.Open("mysql", "godemo:godemo@tcp(127.0.0.1:3306)/godb?charset=utf8")

	selectUserSql := "select account from chat_account where nickname = ?"
	selectRow, _ := db.Query(selectUserSql, nickname)
	var selectAccount int64

	for selectRow.Next() {
		selectRow.Scan(&selectAccount)
	}
	if selectAccount == 0 {
		selectAccount = accountNum + 2
		addUserSql := "insert into chat_account (account, password, nickname) values(?, ?, ?) "
		db.Exec(addUserSql, selectAccount, password, nickname)
	}


}

func Login(account string, password string) bool {
	// chat login --account=747366  --password=password2022
	// Login account: 747366  password: password2022
	fmt.Println("Login account:", account, " password:", password)

	db, _ := sql.Open("mysql", "godemo:godemo@tcp(127.0.0.1:3306)/godb?charset=utf8")

	checkUserSql := "select account from chat_account where account = ? and password = ?"
	selectRow,_ := db.Query(checkUserSql, account, password)
	var queryAccount int
	for selectRow.Next() {
		selectRow.Scan(&queryAccount)
	}

	accountNum, _ := strconv.Atoi(account)
	if queryAccount > 0  && queryAccount == accountNum {
		return true
	} else {
		return false
	}
}

func List(page int) {
	// chat list --page=21
	// List page: 21
	db, _ := sql.Open("mysql", "godemo:godemo@tcp(127.0.0.1:3306)/godb?charset=utf8")
	listSql := "select * from chat_record limit 21"
	selectRow, _ := db.Query(listSql)
	var id int64
	var speaker string
	var listener string
	var message string
	for selectRow.Next() {
		selectRow.Scan(&id, &speaker, &listener, &message)
	}

	fmt.Println("List page:", page)
}

func With(account string) {
	// chat with --account=38474
	// With account: 38474
	fmt.Println("With account:", account)

	db, _ := sql.Open("mysql", "godemo:godemo@tcp(127.0.0.1:3306)/godb?charset=utf8")
	listSql := "select * from chat_record where speaker = ? limit 21"
	selectRow, _ := db.Query(listSql, account)
	var id int64
	var speaker string
	var listener string
	var message string
	for selectRow.Next() {
		selectRow.Scan(&id, &speaker, &listener, &message)
	}
}

func History(with int64) {
	// chat history --with=38474
	// History with: 38474
	fmt.Println("History with:", with)

	db, _ := sql.Open("mysql", "godemo:godemo@tcp(127.0.0.1:3306)/godb?charset=utf8")
	listSql := "select * from chat_record limit 21"
	selectRow, _ := db.Query(listSql)
	var id int64
	var speaker string
	var listener string
	var message string
	for selectRow.Next() {
		selectRow.Scan(&id, &speaker, &listener, &message)
	}
}

func Subscribe() {
	// chat subscribe
	// Subscribe
	fmt.Println("Subscribe")
	db, _ := sql.Open("mysql", "godemo:godemo@tcp(127.0.0.1:3306)/godb?charset=utf8")
	listSql := "select * from chat_record limit 21"
	selectRow, _ := db.Query(listSql)
	var id int64
	var speaker string
	var listener string
	var message string
	for selectRow.Next() {
		selectRow.Scan(&id, &speaker, &listener, &message)
	}
}

func ExtractParam(inputParam string, paramName string) string {
	prefix := "--" + paramName + "="
	if strings.HasPrefix(inputParam, prefix) {
		paramValue := strings.Replace(inputParam, prefix, "" ,1)
		return paramValue
	} else {
		return ""
	}
}


func DealLine() {
	// chat register --nickname=test2022 --password=password2022
	// chat login --account=747366  --password=password2022
	// chat list --page=21
	// chat with --account=38474
	// chat history --with=38474
	// chat subscribe

	reader := bufio.NewReader(os.Stdin)
	lineBytes, _, err := reader.ReadLine()
	if err != nil {
		fmt.Println("read line error", err)
	} else {
		lineString := string(lineBytes)
		// fmt.Println("read line:", lineString)
		// 假设都是按照命令顺序 暂时不考虑 --password 写在 --nickname之前
		inputArray := strings.Fields(lineString)
		// fmt.Println("input param:", inputArray)
		if len(inputArray) >= 2 {
			commandName := inputArray[0]
			if "chat" == commandName {
				actionName := inputArray[1]
				if "register" == actionName {
					// chat register --nickname=test2022 --password=password2022
					nickname := ExtractParam(inputArray[2], "nickname")
					password := ExtractParam(inputArray[3], "password")
					Register(nickname, password)
				} else if "login" == actionName {
					// chat login --account=747366  --password=password2022
					account := ExtractParam(inputArray[2], "account")
					password := ExtractParam(inputArray[3], "password")
					Login(account, password)
				} else if "list" == actionName {
					// chat list --page=21
					pageString := ExtractParam(inputArray[2], "page")
					page, _ := strconv.Atoi(pageString)
					List(page)
				} else if "with" == actionName {
					// chat with --account=38474
					account := ExtractParam(inputArray[2], "account")
					With(account)
				} else if "history" == actionName {
					// chat history --with=38474
					withString := ExtractParam(inputArray[2], "with")
					with, _:= strconv.ParseInt(withString, 10, 64)
					History(with)
				} else if "subscribe" == actionName {
					// chat subscribe
					Subscribe()
				} else if "" == actionName {
					fmt.Println("error actionName:", actionName)
				}
			} else {
				fmt.Println("error command: ", commandName)
			}
		} else {
			fmt.Println("input array size:", len(inputArray))
		}
	}
}

func main() {

	BackUpToFile()
	for size := 1; size < 10000; size++ {
		DealLine()
	}
}
